package com.ruoyi.dataserver.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.dataserver.dto.GhlineDto;
import com.ruoyi.dataserver.service.IGhAnalyzeService;
import com.ruoyi.dataserver.utils.R;
import com.ruoyi.dataserver.api.dto.PlanningDTO;
import com.ruoyi.dataserver.model.GHLINE;
import com.ruoyi.dataserver.service.PlanningService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName
 * @Package com.com.ruoyi.controller
 * @ClassName GhAnalyzeController
 * @Description 规划分析
 * @Company lab
 * @Author wangzhx
 * @Date 2022/3/3  18:03
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/ghAnalyzeController")
@Slf4j
public class GhAnalyzeController {

    @Autowired
    private IGhAnalyzeService ghAnalyzeService;

    @Autowired
    private PlanningService planningService;

    /**
     * @Title  规划数据添加接口
     * @Description
     * @return
     * @author wangw
     * @date 2022/3/11 10:57
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(@RequestBody GhlineDto dto){
        String result = ghAnalyzeService.add(dto);
        return result;
    }
    /**
     * @Title  规划数据删除接口
     * @Description
     * @return
     * @author wangw
     * @date 2022/3/11 10:57
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public String delete(@RequestBody Map map){
        String  id = map.get("id").toString();
        String result = ghAnalyzeService.delete(id);
        return result;
    }

    /**
     * @Title  规划数据列表查询接口
     * @Description
     * @return
     * @author wangw
     * @date 2022/3/11 10:57
     */
    @RequestMapping(value = "/serach",method = RequestMethod.POST)
    public R serach(){
        List<GHLINE> list = ghAnalyzeService.search();
        return R.ok(list);
    }
    /**
     * @Title  规划数据分析接口
     * @Description
     * @return
     * @author wangw
     * @date 2022/3/11 10:57
     */
    @RequestMapping("/anylyze")
    public R anylyze(@RequestBody PlanningDTO planningDTO){
        List res = new ArrayList();
        String targetId = planningDTO.getTarget();
        GHLINE targetLineInfo = ghAnalyzeService.queryById(targetId);
        if (ObjectUtil.isEmpty(targetLineInfo)){
            return R.failed("管线不存在");
        }
        //碰撞检测
        for (String refer :  planningDTO.getRefers()){
            String[] referArray = refer.split("-");
            Map referLineInfo = planningService.getLineInfo(referArray[1],referArray[0]);
            if (ObjectUtil.isEmpty(referLineInfo)){
                return R.failed("管线不存在");
            }
            if(referLineInfo.get("GJ") == null){
                continue;
            }
            // 获取水平实际最短距离
            double distanceHorizontal = planningService.getHorizontalDistance(targetLineInfo.getStartpoint(),targetLineInfo.getEndpoint(),targetLineInfo.getGj(),
                    referLineInfo.get("SNODID").toString(),referLineInfo.get("ENODID").toString(),referLineInfo.get("GJ").toString());

            // 合法水平距离
            double legalDistanceHorizontal = planningService.getHorizontalLegalDistance(targetLineInfo.getDm(),Integer.parseInt(targetLineInfo.getMslx()),
                    referLineInfo.get("DM").toString(),Integer.parseInt(referLineInfo.get("MSLX").toString()));

            // 获取垂直实际最短距离
            double distanceVertical = planningService.getVerticalDistance(new Double(targetLineInfo.getQdms()),new Double(targetLineInfo.getZdms()),
                    new Double(referLineInfo.get("QDMS").toString()),new Double(referLineInfo.get("ZDMS").toString()));

            // 合法垂直距离
            double legalDistanceVertical = planningService.getVerticalLegalDistance(targetLineInfo.getDm(),Integer.parseInt(targetLineInfo.getMslx()),
                    referLineInfo.get("DM").toString(),Integer.parseInt(referLineInfo.get("MSLX").toString()));

            // 如果完全合法,跳过
            /*if (distanceHorizontal >= legalDistanceHorizontal &&
                    distanceVertical >= legalDistanceVertical) {
                continue;
            }*/
            if (distanceHorizontal < legalDistanceHorizontal &&
                    distanceVertical < legalDistanceVertical) {
                Map map = new HashMap();
                map.put("type","碰撞检测");

                map.put("reason","水平净距："+String.format("%.2f",distanceHorizontal)+";合理水平净距："+legalDistanceHorizontal+";垂直净距："+String.format("%.2f",distanceVertical)+";合理垂直净距："+legalDistanceVertical+".");
                map.put("target",planningDTO.getTarget());
                map.put("refer",refer);
                map.put("xzb",referLineInfo.get("SNODID").toString());
                map.put("yzb",referLineInfo.get("ENODID").toString());
                res.add(map);
            }else{
                continue;
            }

           /* Map map = new HashMap();
            map.put("type","碰撞检测");

           // map.put("reason","水平净距："+String.format("%.2f",distanceHorizontal)+";合理水平净距："+String.format("%.2f",legalDistanceHorizontal)+";垂直净距："+String.format("%.2f",distanceVertical)+";合理垂直净距："+String.format("%.2f",legalDistanceVertical)+".");
            map.put("reason","水平净距："+String.format("%.2f",distanceHorizontal)+";合理水平净距："+legalDistanceHorizontal+";垂直净距："+String.format("%.2f",distanceVertical)+";合理垂直净距："+legalDistanceVertical+".");
            map.put("target",planningDTO.getTarget());
            map.put("refer",refer);
            res.add(map);*/

        }
        //覆土检测
        double standardDistance = planningService.getCoverLegalDistance(targetLineInfo.getDm(),Integer.parseInt(targetLineInfo.getMslx()));
        // 合规的跳过
        double qdms = 0,zdms = 0;
        if (ObjectUtil.isEmpty(targetLineInfo.getQdms())){
            qdms = 0;
        }else{
            qdms = Double.parseDouble(targetLineInfo.getQdms());
        }
        if (ObjectUtil.isEmpty(targetLineInfo.getZdms())){
            zdms = 0;
        }else {
            zdms = Double.parseDouble(targetLineInfo.getZdms());
        }
        if (qdms < standardDistance || zdms < standardDistance){
            Map map = new HashMap();
            map.put("type","覆土检测");
            map.put("reason","起点埋深："+targetLineInfo.getQdms()+";终点埋深："+targetLineInfo.getZdms()+";合理埋深："+standardDistance+".");
            map.put("target",planningDTO.getTarget());
            map.put("refer","");
            res.add(map);
        }

        return R.ok(res);
    }
}
